package com.hoas.comparedb.pojo;

import com.hoas.comparedb.entity.TableColumn;
import com.hoas.comparedb.util.StrTool;

public class TableColumnDiffItem extends DiffItem<TableColumn> {

    public static final String NO = "NO";
    public static final String ADD = "ADD";
    public static final String DROP = "DROP";
    public static final String MODIFY = "MODIFY";
    public static final String CHANGE = "CHANGE";

    public TableColumnDiffItem(TableColumn ot, TableColumn nt) {
        super(ot, nt);

        if (nt != null) {
            setKey( nt.getColumnName() );
        } else if( ot != null) {
            setKey( ot.getColumnName() );
        }

        if (ot != null && nt == null ) {
            // 删除字段
            setType(DROP);
        } else if(ot == null && nt != null) {
            // 添加字段
            setType(ADD);

        } else if (ot.getColumnName().equals(nt.getColumnName())){
            // 修改字段
            setType(MODIFY);

            if (    StrTool.equals(ot.getColumnType(), nt.getColumnType())
                    && StrTool.equals(ot.getColumnDefault(), nt.getColumnDefault())
                    && StrTool.equals(ot.getIsNullable(), nt.getIsNullable())
                    && StrTool.equals(ot.getExtra(), nt.getExtra())
                    && StrTool.equals(ot.getColumnComment(), nt.getColumnComment())
                    && StrTool.equals(ot.getCharacterSetName(), nt.getCharacterSetName())
                    && StrTool.equals(ot.getCollationName(), nt.getCollationName())
            ) {
                setType( NO );
            }

        } else {
            // 修改字段
            setType(CHANGE);
        }

    }

}
